package com.ktwlsoft.archivemanage.domain.business.businessService;

import com.google.common.collect.Lists;
import com.ktwlsoft.archivemanage.config.exception.ResultException;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.BasisBusinessManageRepositoryService;
import com.ktwlsoft.archivemanage.domain.dao.entity.Account;
import com.ktwlsoft.archivemanage.domain.dao.entity.BasisBusinessManage;
import com.ktwlsoft.archivemanage.payload.guiBusiness.BasisBusinessManageRequest;
import com.ktwlsoft.archivemanage.service.utils.SnowFlake;
import lombok.Getter;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.List;

/**
 * 依据业务管理
 *
 * @author Name on 2020/4/29 11:28
 * @version 1.0
 */
@Component
public class BasisBusinessManageService {

    /**依据业务管理*/
    @Getter
    private final BasisBusinessManageRepositoryService repositoryService;

    public BasisBusinessManageService(BasisBusinessManageRepositoryService repositoryService) {
        this.repositoryService = repositoryService;
    }

    /**
     * 处理依据数据 on 2020/4/29 12:20
     *
     * @param manageList manageList
     * @param businessId businessId
     * @param account account
     * @return int
     */
    public int handleBasis(List<BasisBusinessManageRequest> manageList, Long businessId, Account account,String type) throws ResultException {
        if(manageList == null || manageList.size() <= 0){
            return 0;
        }
        List<BasisBusinessManage> save = Lists.newArrayList();
        List<Long> delete= Lists.newArrayList();
        for(BasisBusinessManageRequest manage:manageList){
            if(manage.getType() == null){
                continue;
            }
            if(manage.getType().equals(1)){
                manage.setId(SnowFlake.getNextId());
                manage.setBusinessId(businessId);
                manage.setBasisType(type);
                manage.setCreateId(account.getId());
                manage.setCreateName(account.getRealName());
                manage.setCreateTime(new Date());
                BasisBusinessManage businessManage = BasisBusinessManage.builder().build();
                BeanUtils.copyProperties(manage,businessManage);
                try {
                    businessManage.setContent(manage.getContent().getBytes("UTF-8"));
                }catch (Exception e){
                    e.printStackTrace();
                    throw new ResultException("依据描述转utf8报错");
                }
                save.add(businessManage);
            }else if(manage.getType().equals(2)){
                delete.add(manage.getId());
            }
        }
        if(save.size() > 0){
            repositoryService.manyInsert(save);
        }
        if(delete.size() > 0){
            repositoryService.deleteByIds(delete);
        }
        return 1;
    }
}